package com.avic.rong.activity;

import android.content.Intent;
import android.os.Bundle;

import com.avic.rong.MainActivity;
import com.avic.rong.R;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.SharedPreferencesUtil;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;


public class StartUpActivity extends RxAppCompatActivity {

    private Timer timer = new Timer();

    private TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            if (SharedPreferencesUtil.getInstance().getAsBoolean(ConstantsUtils.START_UP_FIRST, false)) {
                Intent intent = MainActivity.queryIntent(StartUpActivity.this);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);//主页

            } else {
                startActivity(BootstrapActivity.getIntent(StartUpActivity.this));//引导页
            }
            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_up);

        //处理首次安装点击打开切到后台,点击桌面图标再回来重启的问题及通过应用宝唤起在特定条件下重走逻辑的问题
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            // Activity was brought to front and not created,
            // Thus finishing this will get us to the last viewed activity
            finish();
            return;
        }

        timer.schedule(timerTask, 2000);

        //华为nova3 手机bug
        if (!isTaskRoot()) {
            finish();
            return;
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer != null) {
            timer.cancel();
        }
    }

}
